package com.yserp.project.erp.order.productbom.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.productbom.domain.ProductionBom;
import com.yserp.project.erp.order.productbom.domain.ProductionBomDto;
import com.yserp.project.erp.order.productbom.domain.ProductionBomVo;
import com.yserp.project.erp.order.productbom.service.IProductionBomService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * BOM主Controller
 *
 * @author yserp
 * @date 2022-08-09
 */
@Controller
@RequestMapping("/order/productbom")
public class ProductionBomController extends BaseController {
    private String prefix = "erp/order/productbom";

    @Autowired
    private IProductionBomService productionBomService;

    @RequiresPermissions("order:productbom:view")
    @GetMapping()
    public String productbom() {
        return prefix + "/productbom";
    }

    /**
     * 查询BOM主列表
     */
    @RequiresPermissions("order:productbom:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductionBom productionBom) {
        startPage();
        List<ProductionBom> list = productionBomService.selectProductionBomList(productionBom);
        return getDataTable(list);
    }

    /**
     * 导出BOM主列表
     */
    @RequiresPermissions("order:productbom:export")
    @Log(title = "BOM主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductionBom productionBom) {
        List<ProductionBom> list = productionBomService.selectProductionBomList(productionBom);
        ExcelUtil<ProductionBom> util = new ExcelUtil<ProductionBom>(ProductionBom.class);
        return util.exportExcel(list, "BOM管理数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.BOM.getPref()));
    }


    /**
     * 新增BOM主
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("bomCode", OrderEnums.getCacheOrderNo(OrderEnums.BOM.getPref()));
        return prefix + "/add";
    }

    /**
     * 新增保存BOM主
     */
    @RequiresPermissions("order:productbom:add")
    @Log(title = "BOM主", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductionBomDto productionBom) {
        return toAjax(productionBomService.insertProductionBom(productionBom));
    }

    /**
     * 修改BOM主
     */
    @RequiresPermissions("order:productbom:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductionBomVo productionBomVo = productionBomService.selectProductionBomById(id);
        mmap.put("productionBomVo", productionBomVo);
        return prefix + "/edit";
    }

    /**
     * 修改保存BOM主
     */
    @RequiresPermissions("order:productbom:edit")
    @Log(title = "BOM主", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductionBomDto productionBom) {
        return toAjax(productionBomService.updateProductionBom(productionBom));
    }

    /**
     * 删除BOM主
     */
    @RequiresPermissions("order:productbom:remove")
    @Log(title = "BOM主", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productionBomService.deleteProductionBomByIds(ids));
    }
}
